<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="种类" prop="siteId">
        <el-select v-model="queryParams.category" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="teamName">
        <el-select v-model="queryParams.isDel" placeholder="请选择">
          <el-option
            v-for="item in isDeloptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['user:team:add']"
        >新增
        </el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['user:team:remove']"
        >删除
        </el-button>
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="warning"-->
      <!--          plain-->
      <!--          icon="el-icon-download"-->
      <!--          size="mini"-->
      <!--          @click="handleExport"-->
      <!--          v-hasPermi="['system:team:export']"-->
      <!--        >导出-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="teamList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column type="index" align="center"></el-table-column>
      <el-table-column label="服务名称" align="center" prop="name"/>
      <el-table-column label="种类" align="center" prop="category">
        <template slot-scope="scope">
          <span>{{ scope.row.category==0?'服务类别':scope.row.category==1?'服务对象':'志愿者保障' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="isDel">
        <template slot-scope="scope">
          <el-tag>{{ scope.row.isDel==0?'正常':'已删除' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row,scope.$index)"
            v-hasPermi="['user:team:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['user:team:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改志愿者队伍对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="服务名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入队伍名称"/>
        </el-form-item>
        <el-form-item label="服务站点" prop="siteId">
          <el-select v-model="form.category" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="显示状态" prop="isDel">
          <el-switch
            v-model="form.isDel"
            active-color="#13ce66"
            :active-value="0"
            :inactive-value="1">
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { delTeam, addTeam, updateTeam, getTeamPage, getSite } from '@/api/system/team'
import {
  activityServicelist,
  addactivityService,
  delActivityService,
  updateactivityService
} from '@/api/activity/activity'

export default {
  name: 'Team',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 志愿者队伍表格数据
      teamList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        category:null,
        name:null,
        isDel:null,
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        category: [
          { required: true, message: '请选择种类', trigger: 'change' }
        ],
        name: [
          { required: true, message: '请输入队伍名称', trigger: 'change' }
        ]
      },
      //站点
      siteList: [],
      options:[{
        value:0,
        label:'服务类别'
      },{
        value:1,
        label:'服务对象'
      },{
        value:2,
        label:'志愿者保障'
      }],
      isDeloptions:[{
        value:0,
        label:'显示'
      },{
        value:1,
        label:'不显示'
      }]
    }

  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询志愿者队伍列表 */
    getList() {
      this.loading = true
      activityServicelist(this.queryParams).then(response => {
        console.log(response.rows)
        this.teamList = response.pageData
        this.total =Number(response.total)
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        pkId: null,
        category: 0,
        isDel: 0,
        name: ""
      }
      // this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        siteId: null,
        teamName: null
      },
        this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.pkId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加志愿者队伍'
    },
    /** 修改按钮操作 */
    handleUpdate(row, index) {
      this.reset()
      const pkId = row.pkId || this.ids
      this.form = this.teamList[index]
      console.error(this.form)
      this.open = true
      this.title = '修改志愿者队伍'
      // getTeam(pkId).then(response => {
      //   this.form = response.data
      //   this.open = true
      //   this.title = '修改志愿者队伍'
      // })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          console.error(this.form.siteId instanceof Array)
          this.form.siteId =this.form.siteId instanceof Array? this.form.siteId[this.form.siteId.length - 1]:this.form.siteId
          if (this.form.pkId != null) {
            updateactivityService(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addactivityService(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const pkIds = row.pkId || this.ids
      this.$modal.confirm('是否确认删除该活动服务项？').then(function() {
        return delActivityService(pkIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('user/team/export', {
        ...this.queryParams
      }, `team_${new Date().getTime()}.xlsx`)
    },
    // 跳转队伍人员列表
    toTeamUserList(row){
      console.log('row'+row.teamName)
      this.$router.push({ path: '/user/management', query: { role: '志愿者',teamName:row.teamName } })
    }
  }
}
</script>
